<template>
	<el-drawer title="文档管理列表" v-model="visible" size="60%" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="createFile" v-if="form.createRole>0">发布</el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del" v-if="form.manageRole>0"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="文件名称"></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main>
				<scTable ref="table" :apiObj="apiObj" :params="{itemId:this.form.itemId}" row-key="recordId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="文件名称" prop="fileName" width="200" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="文档分类" prop="titleName" width="100" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="关联文件" prop="" width="250" :show-overflow-tooltip="true">
						<template #default="scope">
							<attach-menu :attachIds="scope.row.attachId" :roleFlag="4" :preview="true" :uid="scope.row.recordId"></attach-menu>
						</template>
					</el-table-column>
					<el-table-column label="文档格式" prop="extName" width="80"></el-table-column>
					<el-table-column label="当前版本" prop="version" width="100"></el-table-column>
					<el-table-column label="查阅次数" prop="clickCount" width="80">
						<template #default="scope">
							{{ scope.row.clickCount }}/次
						</template>
					</el-table-column>
					<el-table-column label="当前状态" prop="fileStatus" width="80">
						<template #default="scope">
							<el-switch v-model="scope.row.fileStatus" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" active-value="0" inactive-value="1"></el-switch>
						</template>
					</el-table-column>
					<el-table-column label="创建人" prop="createUserName" width="100"></el-table-column>
					<el-table-column label="备注" prop="remark" width="150" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="120">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" v-if="form.manageRole>0">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-drawer>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import AttachMenu from '@/views/module/attachMenu'
import saveDialog from './save'
export default {
	emits: ['success', 'closed'],
	components: {
		scSelectFilter,
		AttachMenu,
		saveDialog,
	},
	data() {
		return {
			loading: false,
			dialog: {
				save: false
			},
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "文档状态",
					key: "fileStatus",
					multiple: false,
					options: [{
						value: '',
						label: '全部'
					}, {
						value: '0',
						label: '生效中'
					}, {
						value: '1',
						label: '已作废'
					}],
				}
			],
			form: {
				itemId: "",
				createRole:"",
				manageRole:"",
			},
			apiObj: this.$API.file.fileMap.getFileRecordListForMange,
			visible: false,
			isSaveing: false,
			selection: [],
			search: {
				fileStatus: '',
				date: '',
				keyword: '',
			}
		}
	},
	mounted() {

	},
	methods: {
		createFile()
		{
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open().setData({itemId:this.form.itemId})
			})
		},
		filterChange(data) {
			this.search.date = data.date;
			this.search.fileStatus = data.fileStatus;
			this.$refs.table.upData(this.search)
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		async changeSwitch(val, row) {
			row.fileStatus = row.fileStatus == '1' ? '0' : '1'
			row.$switch_status = true;
			delete row.$switch_status;
			row.fileStatus = val;
			var res = await this.$API.file.fileMap.updateFileMapRecord.post({recordId: row.recordId, fileStatus: row.fileStatus});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//删除
		async table_del(row) {
			var res = await this.$API.file.fileMap.deleteFileMapRecord.post({recordId: row.recordId});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success(res.message)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var recordIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					recordIdArr.push(this.selection[i].recordId);
				}
				const loading = this.$loading();
				var res = await this.$API.file.fileMap.deleteFileMapRecordByIds.post({ids: recordIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		//显示
		open() {
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			this.form.itemId = data.itemId
			this.form.createRole = data.createRole
			this.form.manageRole = data.manageRole
		},
	}
}
</script>

<style>
</style>
